<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends User_Controller {

    public function __construct()
    {
        parent::__construct();
   	    $this->load->Model('User_model');
   	    $this->load->library('form_validation');


    }


    public function UserList()
    {

        //$this->load->database();
        //$this->load->Model('User_model');
        //$this->load->library('pagination');
        //$perPage=15;
        //$config['base_url']=base_url('admin/user/userlist');
        //$config['per_page']=$perPage;
        //$config['total_rows']=$this->db->count_all_results('user');
        //$config['uri_segment']=4;
        //$this->pagination->initialize($config);
        //$data['links']=$this->pagination->create_links();
        //$offset=$this->uri->segment(4);
        //$this->db->limit($perPage,$offset);


        //$cupge=$this->input->get('page');
        //$cupge=empty($cupge) || $cupge=="0"?1:$cupge;
        //$this->load->library('page');
        //$p=new Page();

        //$data['userList']=$this->User_model->get_alluser();
        //$p->init($config['total_rows'],$perPage,$cupge,5,site_url('admin/user/userlist'));
        //$data['links']=$p->showPages(1);


        //$data['userList']=$this->User_model->get_alluser();



        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;

        $pagesize=10;
        $one=$this->User_model->get_page($cupge,$pagesize,"*","user","order by user_id desc","user_id","");


        $this->load->library('page');
        $p=new Page();

        $data['userList']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/user/userlist'));
        $data['links']=$p->showPages(1);



        //p($p->return_code(site_url('admin/user/userlist')."?key=123"));
        //p($p->showPages(1));exit;

        $this->load->view('Admin/user/userlist.html',$data);

    }
    public function AddUser()
    {
        $this->load->view('Admin/user/adduser.html');
    }
    public function Add_User()
    {
        $valiRes= $this->form_validation->run('user_vali');
        $this->load->library('ajaxMsgModel');
        if($valiRes)
        {
            $this->User_model->user_account=$this->input->post('user_account');
            $this->User_model->user_name=$this->input->post('user_name');
            $this->User_model->user_pass=md5($this->input->post('user_pass'));
            $this->User_model->user_tel=empty($this->input->post('user_tel'))?"":$this->input->post('user_tel');
            $this->User_model->user_phone=$this->input->post('user_phone');
            $this->User_model->user_gender=$this->input->post('user_gender');
            $this->User_model->user_admin=2;
            $userModel=$this->User_model;
            $res= $this->User_model->inset_user($userModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';

            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function Get_UserList()
    {
        $this->load->Model('User_model');
        $this->User_model->Get_UserList();
    }

    public function EditUser()
    {
    	$userId=$this->uri->segment(4);
    	$data['userModel']=$this->User_model->get_user_by_id($userId);
        $this->load->view('Admin/user/edituser.html',$data);
    }

    public function edit_user()
    {
        $valiRes= $this->form_validation->run('edituser_vali');
        $this->load->library('ajaxMsgModel');
        $this->load->library('session');
        if($valiRes)
        {
            $this->User_model->user_id=$this->input->post('user_id');
            $this->User_model->user_account=$this->input->post('user_account');
            $this->User_model->user_name=$this->input->post('user_name');
            $this->User_model->user_pass=md5($this->input->post('user_pass'));
            $this->User_model->user_tel=$this->input->post('user_tel');
            $this->User_model->user_phone=$this->input->post('user_phone');
            $this->User_model->user_gender=$this->input->post('user_gender');
            $userModel=$this->User_model;
          try
          {
              $res=$this->User_model->edit_user($userModel);
              if($res)
              {
                  $this->ajaxmsgmodel->Msg='修改成功-'.$res;
                  $this->ajaxmsgmodel->Statu='ok';

              }
              else
              {
                  $this->ajaxmsgmodel->Msg='修改失败-'.$res;
                  $this->ajaxmsgmodel->Statu='err';
              }
          }
          catch( Exception $ex)
          {
              $this->ajaxmsgmodel->Msg='修改失败-'.$ex;
              $this->ajaxmsgmodel->Statu='err';
          }

        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }


    public function DelUser()
    {
        $this->load->library('ajaxMsgModel');
        $userId=$this->uri->segment(4);
        $backurl=$this->input->get('returnurl');
        $res=$this->User_model->DelUser_ById($userId);
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function Batchdel_User()
    {
        $user_ids=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $userModel=new $this->User_model();
        $user_idsArray=explode('|',trim($user_ids,'|'));
        if(count($user_idsArray)>0)
        {
            $res=$userModel->BatchDel_User($user_idsArray);
            if($res>0)
            {
                $this->ajaxmsgmodel->Msg='操作成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=$backurl;
            }
            else
            {
                $this->ajaxmsgmodel->Msg='操作失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }else
        {
            $this->ajaxmsgmodel->Msg='请选择删除的记录';
            $this->ajaxmsgmodel->Statu='err';
        }

        echo(json_encode($this->ajaxmsgmodel));
    }


    public function check_unique()
    {

        $val=$this->input->get('user_account');
        $mark=$this->input->get('mark');
        $res='true';

        if($mark=='1'){
            $ures=$this->User_model->CheckAddUserName($val);
            if($ures>1){
                $res='false';
            }else{
                $res='true';
            }
        }else{
            $ures=$this->User_model->CheckEditUserName($val);
            if($ures>1){
                $res='false';
            }else{
                $res='true';
            }
        }
        echo $res;
    }
}
